package linknode;

public class LinkNodeTraverse {
    public static void LinkNodeTraversePrint(int[] list) {
        // 构建链表
        LinkNode head = buildLinkNode(list);
        // 遍历链表
        linkNodeTraverse(head);

    }

    // 迭代遍历
    private static void linkNodeTraverse(LinkNode head) {
        // 遍历链表
        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        }
    }

    // 递归遍历
    private static void linkNodeTraverse(LinkNode head, LinkNode tail) {
        if (head == tail) {
            return;
        }
        System.out.println(head.data);
        linkNodeTraverse(head.next, tail);
    }


    public static LinkNode buildLinkNode(int[] list) {
        LinkNode head = new LinkNode(list[0]);
        LinkNode temp = head;
        for (int i = 1; i < list.length; i++) {
            temp.next = new LinkNode(list[i]);
            temp = temp.next;
        }
        return head;
    }


    public static void main(String[] args) {

        int[] list = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        LinkNodeTraversePrint(list);
    }

}
